
package com.yimixian.app.adapter;

import com.yimixian.app.R;
import com.yimixian.app.model.ValidatedOrder;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.ArrayList;

public class ChooseCouponAdapter extends ArrayListAdapter<ValidatedOrder.Bonus> {

    public ChooseCouponAdapter(Activity context) {
        super(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = mContext.getLayoutInflater();
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.adapter_coupon_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (mList.get(position).isChecked()) {
            viewHolder.mCbCoupon.setImageResource(R.drawable.checkbox_p);
        } else {
            viewHolder.mCbCoupon.setImageResource(R.drawable.checkbox_n);
        }
        viewHolder.mTvFruitCouponPrice.setText(mList.get(position).getDiscount() + "");
        viewHolder.mTvFruitCouponTitle.setText(mList.get(position).getTitle());
        viewHolder.mTvFruitCouponTitleSub.setText(mList.get(position).getSubTitle());
        viewHolder.mTvTime.setText(mList.get(position).getDuration());
        if (mList.get(position).isUsable()) {
            viewHolder.mCbCoupon.setVisibility(View.VISIBLE);
            viewHolder.mTvFruitCouponYuan.setTextColor(mContext.getResources().getColor(
                    R.color.ymx_orange));
            viewHolder.mTvFruitCouponPrice.setTextColor(mContext.getResources().getColor(
                    R.color.ymx_orange));
            viewHolder.mTvFruitCouponTitle.setTextColor(mContext.getResources().getColor(
                    R.color.coupon_grey));
            viewHolder.mTvFruitCouponTitleSub.setTextColor(mContext.getResources().getColor(
                    R.color.coupon_grey));
            viewHolder.mTvTime.setTextColor(mContext.getResources().getColor(R.color.coupon_grey));
            convertView.setBackgroundResource(R.drawable.bg_coupon_item_select);

        } else {
            viewHolder.mCbCoupon.setVisibility(View.INVISIBLE);
            viewHolder.mTvFruitCouponYuan.setTextColor(mContext.getResources().getColor(
                    R.color.coupon_disable_grey));
            viewHolder.mTvFruitCouponPrice.setTextColor(mContext.getResources().getColor(
                    R.color.coupon_disable_grey));
            viewHolder.mTvFruitCouponTitle.setTextColor(mContext.getResources().getColor(
                    R.color.coupon_disable_grey));
            viewHolder.mTvFruitCouponTitleSub.setTextColor(mContext.getResources().getColor(
                    R.color.coupon_disable_grey));
            viewHolder.mTvTime.setTextColor(mContext.getResources().getColor(
                    R.color.coupon_disable_grey));
            convertView.setBackgroundResource(R.drawable.bg_coupon_item_select_no);

        }
        return convertView;
    }

    public ArrayList<ValidatedOrder.Bonus> setSelectItem(int pos) {
        boolean isChecked = mList.get(pos).isChecked();
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setChecked(false);
        }
        mList.get(pos).setChecked(!isChecked);

        return mList;

    }

    public static class ViewHolder {
        @InjectView(R.id.cb_coupon)
        ImageView mCbCoupon;
        @InjectView(R.id.tv_time)
        TextView mTvTime;
        @InjectView(R.id.tv_fruit_coupon_yuan)
        TextView mTvFruitCouponYuan;
        @InjectView(R.id.tv_fruit_coupon_price)
        TextView mTvFruitCouponPrice;
        @InjectView(R.id.tv_fruit_coupon_title_sub)
        TextView mTvFruitCouponTitleSub;
        @InjectView(R.id.tv_fruit_coupon_title)
        TextView mTvFruitCouponTitle;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
